﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.盘点管理
{
    /// <summary>
    /// 盘点计划
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class TStocktakingPlanController : ControllerBase
    {
        public IStocktakingPlanRepository _service;

        public TStocktakingPlanController(IStocktakingPlanRepository service)
        {
            _service = service;
        }

        /// <summary>
        /// 添加盘点计划表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("CreateStocktakingPlan")]
        public IActionResult CreateStocktakingPlan(StocktakingPlanDto model)
        {
            string uname = HttpContext.User.FindFirstValue("UserName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            model.CreateUserId = uid;
            model.CreateUserName = uname;
            var create = _service.AddStocktakingPlan(model);
            if (create)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败", "500"));
        }

        /// <summary>
        /// 删除盘点单计划
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteStocktakingPlan")]
        public IActionResult DeleteStocktakingPlan(int id)
        {
            if(id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var delete = _service.DeleteStocktakingPlan(id);

            if (delete)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 修改盘点计划单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut("UpdateStocktakingPlan")]
        public IActionResult UpdateStocktakingPlan(StocktakingPlanDto model)
        {
            if(model.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            model.ModifyUserId = uid;
            var update = _service.UpdateStocktakingPlan(model);
            if (update)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        /// <param name="houseId"></param>
        /// <param name="areaId"></param>
        /// <param name="billSn"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetAllStocktakingPlan")]
        public IActionResult GetAllStocktakingPlan(int houseId = 0, int areaId = 0, string? billSn = "", int status = 0,
            int pageIndex = 1, int pageSize = 10)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = _service.GetAllStocktakingPlans(houseId, areaId, billSn, status, pageIndex, pageSize,
                out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功", "200", totalCount));
            }
            return Ok(BaseResult.Result(false, "查询失败", "500", 0));
        }

        /// <summary>
        /// 根据Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetOneStocktakingPlan")]
        public IActionResult GetOneStocktakingPlan(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("Id有误", "400"));
            }

            var getOne = _service.GetStocktakingPlan(id);
            if (getOne != null)
            {
                return Ok(BaseResult.Result(getOne, "获取成功", "200"));
            }

            return Ok(BaseResult.Result(null, "获取失败", "500"));
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut("UpdateStocktakingPlanStatus")]
        public IActionResult UpdateStocktakingPlanStatus(ReceivingOrderDetailDto model)
        {
            if(model.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOk = _service.UpdateState(model);
            if (IsOk)
            {
                return Ok(BaseResult.Result(true, "修改状态成功！", "200"));
            }

            return Ok(BaseResult.Result(false, "修改状态失败！", "200"));
        }
    }
}